using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Xml;

namespace Catchbase.Service.API
{
    public class HttpWebRequestGateway : IStreamProvider
    {
        private HttpWebRequest _request;

        protected HttpWebRequestGateway()
        {
        }

        public HttpWebRequestGateway(HttpWebRequest request)
        {
            Debug.Assert(request != null);
            _request = request;
        }

        public virtual WebHeaderCollection Headers
        {
            get { return _request.Headers; }
        }

        public virtual Uri RequestedUri
        {
            get { return _request.RequestUri; }
        }

        public virtual string Method
        {
            set { _request.Method = value; }
        }

        public virtual string ContentType
        {
            set { _request.ContentType = value; }
        }

        public virtual void Intialize(System.Uri requestUri)
        {
            Debug.Assert(requestUri != null);
            _request = (HttpWebRequest)WebRequest.Create(requestUri);
        }

        public virtual HttpWebResponseGateway GetResponse()
        {
            return new HttpWebResponseGateway((HttpWebResponse)_request.GetResponse());
        }

        public Stream GetStream()
        {
            return _request.GetRequestStream();
        }

        public void ReleaseStream(Stream stream)
        {
            Debug.Assert(stream != null);
            stream.Close();
        }
    }
}